package com.halachawiki.client.login;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.halachawiki.client.login.LoginService.Status;
import com.halachawiki.shared.PermissionException;
import com.halachawiki.shared.PublicUserInfo;

public class UserAdminControl {
	private LoginServiceAsync loginService = GWT.create(LoginService.class);
	private LoginConstants Constants = GWT.create(LoginConstants.class);

	private VerticalPanel root = new VerticalPanel();
	
	public VerticalPanel getRoot() { return root; }
	
	public void draw() {
		root.clear();
		loginService.getAllUserInfo(new AsyncCallback<PublicUserInfo[]>() {

			@Override
			public void onFailure(Throwable caught) {
				// most likely, no permission
				Label label = new Label(Constants.genericError());
				if (caught instanceof PermissionException) {
					label.setText(Constants.permissoinDenied());
				}
				root.clear();
				root.add(label);
			}

			@Override
			public void onSuccess(final PublicUserInfo[] result) {
				FlexTable table = new FlexTable();
				root.clear();
				root.add(table);
				for (int i = 0; i < result.length; i++) {
					table.setWidget(i, 0, new Label(result[i].displayName));
					table.setWidget(i, 1, new Label(result[i].email));
					table.setWidget(i, 2, new Label(result[i].status.toString()));
					drawButton(result[i], table, i, 3);
				}
			} });
	}
	
	void refreshUserAndDrawButton(PublicUserInfo user, final FlexTable table, final int row, final int column) {
		loginService.getUserInfo(user.email, new AsyncCallback<PublicUserInfo>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(PublicUserInfo newUser) {
				drawButton(newUser, table, row, column);
			} });
		
	}
	void drawButton(final PublicUserInfo user, final FlexTable table, final int row, final int column) {
		final Button button = new Button();
		table.setWidget(row, column, button);
		final String email = user.email;
		if (user.status == Status.OK) {
			button.setText(Constants.banButton());
			button.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					loginService.banUser(email, new AsyncCallback<Status>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Status result) {
							Window.alert("User with email " + email + " has been banned from the site");
							refreshUserAndDrawButton(user, table, row, column);
						}
					});
				}
				
			});
		} else if (user.status == Status.USER_NOT_CONFIRMED) { // email not confirmed
			button.setText(Constants.confirmEmailButton());
			button.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					loginService.confirmUserEmail(email, new AsyncCallback<Status>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Status result) {
							Window.alert("User with email " + email + " has skipped email confirmation phase");
							refreshUserAndDrawButton(user, table, row, column);
						} 
					});
				}
				
			});

		} else if (user.status == Status.USER_NOT_APPROVED) {
			button.setText(Constants.approveUserButton());
			button.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					loginService.approveUser(email, new AsyncCallback<Status>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Status result) {
							Window.alert("User with email " + email + " has beem approved.");
							refreshUserAndDrawButton(user, table, row, column);
						} 
					});
			
				
				}
			});
			
		} else if (user.status == Status.USER_BANNED) {
			button.setText(Constants.unbanUserButton());
			button.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					loginService.unbanUser(email, new AsyncCallback<Status>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Status result) {
							Window.alert("User with email " + email + " is no longer banned.");
							refreshUserAndDrawButton(user, table, row, column);
						} 
					});
			
				
				}
			});
		}
	}
}
